//
// Created by Tony on 2024/6/20.
//

#include "FunctionParser.h"

#include "../common/KeyWords.h"
#include "../common/Utils.h"
#include "ast/Ast.h"
#include "ast/list/function/FunctionDefine.h"
#include <memory>

namespace Parser {

using std::make_shared;

shared_ptr<Ast> FunctionParser::parse() { return this->functionDefine(); }

/**
 * function_define := "function" name parameter_list type_tag block
 */
shared_ptr<FunctionDefine> FunctionParser::functionDefine() {
    auto ret = skipName(KeyWords::FUNCTION);
    CHECK_FALSE_RETURN(ret, nullptr);

    auto name = this->name();
    CHECK_NULL_RETURN(name, nullptr);

    auto paramList = this->parameterList();
    CHECK_NULL_RETURN(paramList, nullptr);

    auto typeTag = this->typeTag();
    CHECK_NULL_RETURN(typeTag, nullptr);

    auto block = this->block();
    CHECK_NULL_RETURN(block, nullptr);

    return make_shared<FunctionDefine>(vector<shared_ptr<Ast>>{name, paramList, typeTag, block});
}

} // namespace Parser